package com.oa.admin.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.joneying.common.annotation.CommonService;
import com.joneying.common.web.response.Resp;
import com.oa.admin.dto.HouseAddDTO;
import com.oa.admin.dto.MonitoringAddDTO;
import com.oa.admin.dto.MonitoringUpdateDTO;
import com.oa.admin.exportvo.MonitoringExportVO;
import com.oa.admin.vo.MonitoringDetailsVO;
import com.oa.admin.vo.MonitoringListVO;
import com.oa.core.PageResult;
import com.oa.admin.entity.Monitoring;
import com.oa.admin.mapper.MonitoringMapper;
import com.oa.admin.service.IMonitoringService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.oa.core.enumm.CodeEnum;
import com.oa.core.exception.BusinessException;
import com.oa.core.exception.DataIsEmptyException;
import com.oa.core.utils.DateUtil;
import com.oa.core.utils.ExcelUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

import static com.oa.core.utils.Validate.validate;

/**
 * <p>
 * 监控表 服务实现类
 * </p>
 *
 * @author syb123
 * @since 2019-01-22
 */
@Service
public class MonitoringServiceImpl extends ServiceImpl<MonitoringMapper, Monitoring> implements IMonitoringService {

    private final MonitoringMapper monitoringMapper;
    @Autowired
    private HttpServletResponse response;

    @Autowired
    public MonitoringServiceImpl(MonitoringMapper monitoringMapper) {
        this.monitoringMapper = monitoringMapper;
    }

    @Override
    @CommonService
    public PageResult<Monitoring> findList(Integer pageNum, Integer pageSize) {
        if (pageNum == null) {
            List<Monitoring> list = monitoringMapper.findList();
            return new PageResult<>(list);
        } else {
            // 当前页，总条数 构造 page 对象
            Page<Monitoring> page = new Page<>(pageNum, pageSize);
            //查询
            List<Monitoring> list = monitoringMapper.findList(page);
            return new PageResult<>(list);
        }
    }

    /**
     * 根据id查询
     */
    @Override
    @CommonService
    public Monitoring findMonitoringById(String id) {
        return monitoringMapper.findById(id);
    }

    /**
     * 新增监控
     *
     * @param monitoringAddDTO
     * @return
     */
    @Override
    @CommonService
    public Resp addMonitoring(MonitoringAddDTO monitoringAddDTO) throws BusinessException {

        CheckMonitoringParam(monitoringAddDTO);

        Monitoring monitoring = new Monitoring();

        BeanUtils.copyProperties(monitoringAddDTO, monitoring);

        try {
            insert(monitoring);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("添加监控失败!");
        }


        return new Resp<String>().success("添加监控成功!");
    }

    /**
     * 根据条件查询监控列表
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    @CommonService
    public PageResult<MonitoringListVO> findMonitoringListByCondition(Integer pageNum, Integer pageSize) throws DataIsEmptyException {
        if (pageNum == null) {
            List<Monitoring> list = monitoringMapper.findList();
            if (CollUtil.isEmpty(list)) {
                throw new DataIsEmptyException();
            }
            List<MonitoringListVO> monitoringListVOList = new ArrayList<>();
            list.forEach(x -> {
                MonitoringListVO monitoringListVO = new MonitoringListVO();
                BeanUtils.copyProperties(x, monitoringListVO);
                monitoringListVOList.add(monitoringListVO);
            });
            return new PageResult<>(monitoringListVOList);
        } else {
            Page<Monitoring> page = new Page<>(pageNum, pageSize);
            List<Monitoring> list = monitoringMapper.findList(page);
            if (CollUtil.isEmpty(list)) {
                throw new DataIsEmptyException();
            }
            List<MonitoringListVO> monitoringListVOList = new ArrayList<>();
            list.forEach(x -> {
                MonitoringListVO monitoringListVO = new MonitoringListVO();
                BeanUtils.copyProperties(x, monitoringListVO);
                monitoringListVOList.add(monitoringListVO);
            });
            return new PageResult<>(monitoringListVOList);
        }

    }

    /**
     * 根据id查询监控详情
     *
     * @param id
     * @return
     */
    @Override
    @CommonService
    public Resp findMonitoringDetailsById(String id) throws DataIsEmptyException {
        Monitoring monitoring = monitoringMapper.findById(id);
        if (StringUtils.isEmpty(monitoring)) {
            throw new DataIsEmptyException();
        }
        MonitoringDetailsVO monitoringDetailsVO = new MonitoringDetailsVO();
        BeanUtils.copyProperties(monitoring, monitoringDetailsVO);
        return new Resp<MonitoringDetailsVO>().success(monitoringDetailsVO);
    }

    /**
     * 根据id编辑监控
     *
     * @param monitoringUpdateDTO
     * @return
     */
    @Override
    @CommonService
    public Resp updatMonitoringDetailsById(MonitoringUpdateDTO monitoringUpdateDTO) throws BusinessException {

        Monitoring monitoring = new Monitoring();

        BeanUtils.copyProperties(monitoringUpdateDTO, monitoring);

        try {
            monitoringMapper.updateById(monitoring);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("修改监控失败!");
        }

        return new Resp<String>().success("修改监控成功!");
    }

    /**
     * 导出监控列表
     *
     * @return
     */
    @Override
    @CommonService
    public Resp exportMonitoringList() throws DataIsEmptyException, BusinessException {

        List<Monitoring> monitoringList = monitoringMapper.findList();

        if (CollUtil.isEmpty(monitoringList)) {
            throw new DataIsEmptyException();
        }

        List<MonitoringExportVO> monitoringExportVOList = new ArrayList<>();
        monitoringList.forEach(x -> {
            MonitoringExportVO monitoringExportVO = new MonitoringExportVO();
            BeanUtils.copyProperties(x, monitoringExportVO);
            monitoringExportVO.setResponsibleTime(DateUtil.formatDate(x.getStartTime(), "yyyy-MM-dd") + "--" + DateUtil.formatDate(x.getEndTime(), "yyyy-MM-dd"));
            monitoringExportVOList.add(monitoringExportVO);
        });

        String[] rowName = {"账号", "管理员姓名", "权限", "设备", "负责时间", "职责", "备注"};

        try {
            ExcelUtil.downloadExcel("车辆表" + DateUtil.getDayMonth(), rowName, monitoringExportVOList, "监控表" + DateUtil.getDayMonth() + ".xls", response);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("导出监控excel表格失败!");
        }

        return new Resp<>().success("导出监控excel表格成功!");


    }


    /**
     * 参数校验
     *
     * @param
     */
    public void CheckMonitoringParam(MonitoringAddDTO monitoringAddDTO) throws BusinessException {
        if (!StringUtils.isEmpty(monitoringAddDTO)) {
            List<String> validate = validate(monitoringAddDTO);
            if (CollUtil.isNotEmpty(validate)) {
                throw new BusinessException(CodeEnum.PARAMETERS_OF_THE_ABNORMAL.getCode(), validate.toString());
            }
        }
    }
}
